package com.datn.activity;

import java.util.ArrayList;

import com.datn.DataAccess.FavouriteDatabaseAdapter;
import com.datn.DataAccess.ListFavouriteAdapter;
import com.datn.beans.FavouriteBean;
import com.datn.beans.UserBean;
import com.datn.utils.AlertCustom;
import com.datn.utils.DialogUtils;
import com.datn.utils.PreferenceUtils;
import com.datn.utils.places.Place;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class FavouriteActivity extends Activity implements OnItemClickListener{
	public static final String DELETE_CONTEXT_MENU = "Delete";
	private ArrayList<FavouriteBean> favourites;
	private ListFavouriteAdapter adapter;
	private ListView lvFavourite;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_favourite);
		PreferenceUtils.getInstance(this);
		
		
			updateListViewFavourite();
			lvFavourite.setOnItemClickListener(this);
			lvFavourite.setOnCreateContextMenuListener(this);
	}
	
	public void updateListViewFavourite(){
		lvFavourite = (ListView) findViewById(R.id.lvCategoryList);
		
		FavouriteDatabaseAdapter adapterDatabase=new FavouriteDatabaseAdapter(this);
		adapterDatabase.open();
		
		favourites = adapterDatabase.getFavouritePlaceWithUser(UserBean.username); 		
		adapter = new ListFavouriteAdapter(this, R.layout.item_place, favourites);
		lvFavourite.setAdapter(adapter);
		lvFavourite.invalidate();
		
		adapterDatabase.close();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if (keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent=new Intent(this, NearLocationActivity.class);
			startActivity(intent);
			finish();
		}
		
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		try{
			FavouriteBean bean = favourites.get(arg2);
			if (bean!=null){
				Intent intent=new Intent(this,DetailsPlaceActivity.class);				
					Place place = new Place();
					place.setActualurl(bean.getUrl());
					place.setAddressLine(bean.getAddress());
					//place.setDistance(bean.get)
					place.setLatitude(bean.getLatitude());
					place.setLongtitude(bean.getLongitude());
					place.setName(bean.getNameplace());
					place.setPhonenumber(bean.getPhonenumber());
					
					PreferenceUtils.setPlaceDetails(place);
					PreferenceUtils.setModeOnlinẹ̣(false);
				startActivity(intent);
				finish();
			}			
		}
		catch (Exception e) {
			// TODO: handle exception
			AlertCustom.showAlert(this, "ERROR", "Cannot get details place");
		}
	}
	
	
	@Override
    public void onCreateContextMenu(ContextMenu contextMenu,
                                    View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
		if (v.getId()==R.id.lvCategoryList) {
		    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		    contextMenu.setHeaderTitle("Quick Menu");
		    contextMenu.add(Menu.NONE,0,0,DELETE_CONTEXT_MENU);
		  }
    }
	
	@Override
    public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		FavouriteDatabaseAdapter adapter = new FavouriteDatabaseAdapter(this);
		try{
		
		adapter.open();
			//int result = adapter.deleteRecentPlace(allRecentPlace.get(info.position));
		int result = adapter.deleteFavouritePlace(favourites.get(info.position));
			if (result<=0) {
				Toast.makeText(this, "Delete failed", Toast.LENGTH_LONG).show();
			}
			else{
				Toast.makeText(this, "Delete success", Toast.LENGTH_LONG).show();
				updateListViewFavourite();
			}
			
		adapter.close();
		}
		catch(Exception ex){
			
		} finally {
			try {
				adapter.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
        return false;
    }
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.favourite_menu, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.itemHelpFavouriteMenu:
			processClickHelpMenu();			
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void processClickHelpMenu() {
		// TODO Auto-generated method stub
		DialogUtils.getInstance(this);
		DialogUtils.showCustomHelpDialog(R.layout.help_favourite_menu);
	}

	
	
	
}
